import { Component, OnChanges,OnInit, SimpleChanges, Input } from '@angular/core';
import { HttpServcie } from '../../../service/http.service';
import { Router } from '@angular/router';
import { NzNotificationService } from 'ng-zorro-antd';
import { Observable } from 'rxjs/Observable';

@Component({
    selector: 'superviser-liability',
    templateUrl: 'superviser-liability.html',
    styleUrls: ['superviser-liability.scss']
})

export class SuperviserLiability implements OnInit,OnChanges {

    constructor(
        private httpServcie: HttpServcie,
        private router: Router,
        private _notification: NzNotificationService
    ) { 

    }
    @Input() partyId: string="";

    option1 = {};
    option2 = {};
    option4 = {};
    option5 = {};
    option6 = {};
    option7 = {};
    option8 = {};
    maintNum:any;
    usedNum:any;
    maintCzryTotal:any;
    usedCzryTotal:any;

    ngOnInit() {
        
    }

    ngOnChanges(changes: SimpleChanges) {
        this.countMaintCompany();    //维保单位数量
        this.countUseCompany();        //使用单位数量
        this.countTechmanCompany();        //使用单位、维保单位 资质人员数量。
        this.countStarCompany();        //各星级维保单位数量
        this.countUsedZz();            //使用单位各资质
        this.countMaintZz();            //使用单位各资质
    }


    countMaintZz(){
        let param={
            id: this.partyId
        }
        this.httpServcie.countMaintZz(param).subscribe(data => {
            if(data.status==200){
                if(data.data && (<Array<any>>data.data).length){
                    let nameList = [];
                    let memberList = [];
                    (<Array<any>>data.data).forEach(zzjbOb=>{
                        if(zzjbOb.id == this.partyId && zzjbOb.zzjb){
                            nameList.push(zzjbOb.zzjb+'级');
                            memberList.push({
                                name:zzjbOb.zzjb+'级',
                                value:zzjbOb.zzjb_count
                            });
                        }
                    });
                    this.option8 = {
                        title : {
                            text: '维保单位各资质占比',
                            subtext: '',
                            x:'center'
                        },
                        tooltip : {
                            trigger: 'item',
                            formatter: "{a} <br/>{b} : {c} ({d}%)"
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left',
                            data: nameList
                        },
                        series : [
                            {
                                name: '家',
                                type: 'pie',
                                radius : '30%',
                                center: ['45%', '50%'],
                                data:memberList,
                                itemStyle: {
                                    emphasis: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                }

            }else if (data.status === 401) {
                this.router.navigateByUrl('/login');
                return;
            } else {
            // 异常
            this._notification.create('error', '错误提示', data.message);
            }
        });
    }

    countUsedZz(){
        let param={
            id: this.partyId
        }
        this.httpServcie.countUsedZz(param).subscribe(data => {
            if(data.status==200){
                if(data.data && (<Array<any>>data.data).length){
                    let nameList = [];
                    let memberList = [];
                    (<Array<any>>data.data).forEach(zzjbOb=>{
                        if(zzjbOb.id == this.partyId && zzjbOb.zzjb){
                            nameList.push(zzjbOb.zzjb+'级');
                            memberList.push({
                                name:zzjbOb.zzjb+'级',
                                value:zzjbOb.zzjb_count
                            });
                        }
                    });

                    this.option6 = {
                        title : {
                            text: '使用单位各资质占比',
                            subtext: '',
                            x:'center'
                        },
                        tooltip : {
                            trigger: 'item',
                            formatter: "{a} <br/>{b} : {c} ({d}%)"
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left',
                            data: nameList
                        },
                        series : [
                            {
                                name: '家',
                                type: 'pie',
                                radius : '30%',
                                center: ['45%', '50%'],
                                data:memberList,
                                itemStyle: {
                                    emphasis: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                }

            }else if (data.status === 401) {
                this.router.navigateByUrl('/login');
                return;
            } else {
            // 异常
            this._notification.create('error', '错误提示', data.message);
            }
        });
    }
    

    countUseCompany(){
        let param={
            id: this.partyId
        }
        this.httpServcie.countUseCompany(param).subscribe(data => {
            if(data.status==200){
                let countList = data.data;
                let nameList = [];
                let memberList = [];
                countList.forEach(used => {
                    if(used.id==this.partyId){
                        this.usedNum = used.total_count;
                    }else{
                        let total_count = used.total_count;
                        memberList.push({
                            name:used.party_name,
                            value:total_count
                        });
                        nameList.push(used.party_name);
                    }
                });
    
                this.option2 = {
                    title : {
                        text: '使用单位分布图',
                        subtext: '',
                        x:'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameList
                    },
                    series : [
                        {
                            name: '家',
                            type: 'pie',
                            radius : '30%',
                            center: ['45%', '50%'],
                            data:memberList,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }
        });
           
    }

    countMaintCompany(){
        let param={
            id: this.partyId
        }
        this.httpServcie.countMaintCompany(param).subscribe(data => {
            if(data.status==200){
                let countList = data.data;
                let nameList = [];
                let memberList = [];
                countList.forEach(maint => {
                    if(maint.id==this.partyId){
                        this.maintNum = maint.total_count;
                    }else{
                        let total_count = maint.total_count;
                        memberList.push({
                            name:maint.party_name,
                            value:total_count
                        });
                        nameList.push(maint.party_name);
                    }
                });
    
                this.option1 = {
                    title : {
                        text: '维保单位分布图',
                        subtext: '',
                        x:'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameList
                    },
                    series : [
                        {
                            name: '家',
                            type: 'pie',
                            radius : '30%',
                            center: ['45%', '50%'],
                            data:memberList,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            } else if (data.status === 401) {
                this.router.navigateByUrl('/login');
                return;
              } else {
                // 异常
                this._notification.create('error', '错误提示', data.message);
              }
        });
    }

    countTechmanCompany(){
        let param={
            id: this.partyId
        }
        this.httpServcie.countTechmanCompany(param).subscribe(data => {
            if(data.status==200){
                let countTechmanList = data.data;
                let nameListm=[];
                let memberListm=[];

                let nameListu=[];
                let memberListu=[];
                countTechmanList.forEach(countTechman => {
                    if(countTechman.id==this.partyId){
                        this.maintCzryTotal = countTechman.maint_czry_num;
                        this.usedCzryTotal = countTechman.used_czry_num;
                    }else{
                        memberListm.push({
                            name:countTechman.party_name,
                            value:countTechman.maint_czry_num
                        });
                        nameListm.push(countTechman.party_name);

                        memberListu.push({
                            name:countTechman.party_name,
                            value:countTechman.used_czry_num
                        });
                        nameListu.push(countTechman.party_name);
                    }
                });

                this.option4 = {
                    title : {
                        text: '维保单位持证人员',
                        subtext: '',
                        x:'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameListm
                    },
                    series : [
                        {
                            name: '位',
                            type: 'pie',
                            radius : '30%',
                            center: ['45%', '50%'],
                            data:memberListm,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };

                this.option5 = {
                    title : {
                        text: '使用单位持证人员',
                        subtext: '',
                        x:'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameListu
                    },
                    series : [
                        {
                            name: '位',
                            type: 'pie',
                            radius : '30%',
                            center: ['45%', '50%'],
                            data:memberListu,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }
   
        })
    }


    countStarCompany(){
        let param={
            id: this.partyId
        }
        this.httpServcie.countStarCompany(param).subscribe(data => {
            let startNumMaintList:Array<Object> = data.data;
            let starList=[];
            if(startNumMaintList){
                for(let i=0; i<startNumMaintList.length; i++){
                    if(i==0){
                        startNumMaintList.pop();
                    }
                }
                let star5 = 0;
                let star4 = 0;
                let star3 = 0;
                let star2 = 0;
                let star1 = 0;
                let star0 = 0;
                startNumMaintList.forEach(satrtNumMaint => {
                    if(satrtNumMaint['start_num']==5){
                        star5++
                    }
                    if(satrtNumMaint['start_num']==4){
                        star4++
                    }
                    if(satrtNumMaint['start_num']==3){
                        star3++
                    }
                    if(satrtNumMaint['start_num']==2){
                        star2++
                    }
                    if(satrtNumMaint['start_num']==1){
                        star1++
                    }
                    if(!satrtNumMaint['start_num'] || satrtNumMaint['start_num']==0){
                        star0++
                    }
                });
                starList.push({
                    name:'五星',
                    value:star5
                })
                starList.push({
                    name:'四星',
                    value:star4
                })
                starList.push({
                    name:'三星',
                    value:star3
                })
                starList.push({
                    name:'二星',
                    value:star2
                })
                starList.push({
                    name:'一星',
                    value:star1
                })
                starList.push({
                    name:'零星',
                    value:star0
                })

            }

            this.option7 = {
                title : {
                    text: '维保单位星级占比',
                    subtext: '',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['五星','四星','三星','二星','一星','零星']
                },
                series : [
                    {
                        name: '星级',
                        type: 'pie',
                        radius : '30%',
                        center: ['45%', '50%'],
                        data:starList,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
        });

    }

}
